package vkmc;

import java.io.*;
import java.util.StringTokenizer;
import run.OS;
import run.TextFileStream;

public class Settings
{
	public static final String default_sets = "DOWNLOADS_PATH: %s;\nLANG: %s;\nMIN_BITRATE: %s;\nSYSTEM_LNF: %s;\nPASSWORD: %s;\nUSERNAME: %s;";

	private static String home = System.getProperty("user.home");
	private static String dldir = home + OS.getSlash() + "VKMC";
	private static String cfgfile = "./vkmc";

	private static String downloadDir;
	private static String language;
	private static String maxBitRate;
	private static boolean systemLnF;

	private static String username;
	private static String password;

	public static void Initialize()
	{
		if (pathExists(cfgfile))
		{
			loadData();
			return;
		}
		writeDefaultSettingsFile();
		reload();
	}

	public static String getUsername()
	{
		return rot13i(username);
	}

	public static String getPassword()
	{
		return rot13i(password);
	}
	
	public static void setPassword(String pass)
	{
		password = rot13(pass);
	}
	
	public static void setUsername(String uname)
	{
		username = rot13(uname);
	}

	public static String rot13(String s)
	{
		String r = "";
		for (int i = 0; i < s.length(); i++)
		{
			char c = s.charAt(i);
			if (c >= 'a' && c <= 'm')
				c += 13;
			else if (c >= 'A' && c <= 'M')
				c += 13;
			else if (c >= 'n' && c <= 'z')
				c -= 13;
			else if (c >= 'N' && c <= 'Z')
				c -= 13;
			r += c;
		}
		return r;
	}
	
	private static String rot13i(String r)
	{
		String s = "";
		for (int i = 0; i < r.length(); i++)
		{
			char c = r.charAt(i);
			if (c >= 'n' && c <= 'z')
				c -= 13;
			else if (c >= 'N' && c <= 'Z')
				c -= 13;
			else if (c >= 'a' && c <= 'm')
				c += 13;
			else if (c >= 'A' && c <= 'M')
				c += 13;
			s += c;
		}
		return s;
	}

	public static void reload()
	{
		loadData();
	}

	private static void loadData()
	{
		TextFileStream file = new TextFileStream(Settings.getSettingsPath());
		String data = Settings.generateDefaultSettings(default_sets);
		if (file.exists())
			data = file.getLines().trim();
		StringTokenizer st = new StringTokenizer(data, ";");
		if (st.countTokens() < 4)
		{
			data = Settings.generateDefaultSettings(default_sets);
			st = new StringTokenizer(data, ";");
		}
		
		boolean p = false, u = false;

		while (st.hasMoreTokens())
		{
			String token = st.nextToken().trim();
			String[] d = new String[2];
			d[0] = token.substring(0, token.indexOf(":")).trim();
			d[1] = token.substring(token.indexOf(":") + 1).trim();

			if (d[0].trim().toUpperCase().equals("DOWNLOADS_PATH"))
				downloadDir = d[1].trim();
			else if (d[0].trim().toUpperCase().equals("LANG"))
				language = d[1].trim();
			else if (d[0].trim().toUpperCase().equals("MIN_BITRATE"))
				maxBitRate = d[1].trim();
			else if (d[0].trim().toUpperCase().equals("SYSTEM_LNF"))
				systemLnF = d[1].trim().toLowerCase().equals("true") ? true : false;
			else if (d[0].trim().toUpperCase().equals("PASSWORD"))
			{
				password = d[1].trim();
				p = true;
			}
			else if (d[0].trim().toUpperCase().equals("USERNAME"))
			{
				username = d[1].trim();
				u = true;
			}
		}
		
		if (!p || !u)
		{
			String s = String.format(default_sets, getDownloadDirectory(), getLanguage(), getMinBitrate(), isSystemLookAndFeel() ? "true" : "false", p ? password : rot13("123wlf"), u ? username : rot13("server3share@hotmail.it"));
			TextFileStream f = new TextFileStream(cfgfile);
			f.clear();
			f.addLine(s);
			f.apply();
			reload();
		}
	}

	private static void writeDefaultSettingsFile()
	{
		TextFileStream file = new TextFileStream(cfgfile);
		if (file.exists())
			file.deleteFile();
		file.createFile();
		file.addLine(generateDefaultSettings(default_sets));
		file.apply();
	}

	public static String getSettingsPath()
	{
		return cfgfile;
	}

	public static String generateDefaultSettings(String pattern)
	{
		return String.format(pattern, dldir, "English", "128", "false", rot13("123wlf"), rot13("server3share@hotmail.it"));
	}

	public static String getDownloadDirectory()
	{
		return downloadDir;
	}

	public static String getLanguage()
	{
		return language;
	}

	public static String getMinBitrate()
	{
		return maxBitRate;
	}

	public static boolean isSystemLookAndFeel()
	{
		return systemLnF;
	}

	private static boolean pathExists(String path)
	{
		File file = new File(path);
		return file.exists();
	}

	public static void createDonwloadsDir()
	{
		if (pathExists(downloadDir))
			return;
		File file = new File(downloadDir);
		file.mkdir();
	}

}
